$(function () {
    $("#faultTypeGrid").Grid({
        url: '../faulttype/list',
        rownumWidth: 35,
        colModel: [
            {label: '类型名称', name: 'typeName', index: 'TYPE_NAME', width: 60},
            {
                label: '上级类型', name: 'parentName', index: 'TYPE_PID', width: 60,
                formatter: function (value) {
                    return value === null ? '无' : value;
                }
            },
            {label: '故障编码', name: 'faultCode', index: 'FAULT_CODE', width: 60},
            {
                label: '所属模块', name: 'category', index: 'CATEGORY', width: 60,
                formatter: function (value) {
                    return sysEnums.FAULT_TYPE[value];
                }
            },
            {label: '备注', name: 'remark'},
            {name: 'id', index: 'ID', key: true, hidden: true}
        ]
    });
    vm.getFaultTree();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: function () {
            var node = faultTree.getSelectedNodes();
            vm.q.treeCode = node[0].treeCode;
            vm.query()
        }
    }
};
var faultTree;
var faultTree2;

var setting2 = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    }
};

var vm = new Vue({
    el: '#faultType',
    data: {
        showList: true,
        title: null,
        faultType: {
            parentName: null,
            typePid: '0'
        },
        ruleValidate: {
            // typeName: [
            //     {required: true, message: '故障类型必填！', trigger: 'change'}
            // ],
            // parentName: [
            //     {required: true, message: '上级类型必选！', trigger: 'change'}
            // ],
            // faultCode: [
            //     {required: true, message: '故障代码必填！', trigger: 'change'}
            // ]
        },
        q: {
            name: '',
            id: '',
            category: '',
            treeCode: ''
        },
        faultTree: {},
        TypeArray: enumsToList("FAULT_TYPE")
    },
    created: function () {
        this.getFaultTree();
    },
    methods: {
        getFaultTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../faulttype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.faultList;
                    faultTree = $.fn.zTree.init($("#faultTree"), setting, tree);
                }
            });
        },
        getFaultTypeTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../faulttype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.faultList;
                    faultTree2 = $.fn.zTree.init($("#faultTypeTree"), setting2, tree);

                    var node = faultTree2.getNodeByParam("id", vm.faultType.id);
                    if (node) {
                        faultTree2.selectNode(node);
                        if (node.parentName) {
                            vm.faultType.parentName = node.parentName;
                        } else {
                            vm.faultType.parentName = '无';
                        }

                    } else {
                        node = faultTree2.getNodeByParam("id", "0");
                        faultTree2.selectNode(node);
                    }

                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择故障类型",
                area: ['300px', '450px'],
                content: jQuery("#faultTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = faultTree2.getSelectedNodes();
                    //选择上级部门
                    vm.faultType.typePid = node[0].id;
                    vm.faultType.parentName = node[0].typeName;

                    layer.close(index);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.getFaultTypeTree();
        },
        update: function (event) {
            var id = getSelectedRow("#faultTypeGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
            vm.getFaultTypeTree();
        },
        saveOrUpdate: function (event) {
            var url = vm.faultType.id == null ? "../faulttype/save" : "../faulttype/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.faultType),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var id = getSelectedRow("#faultTypeGrid");
            debugger
            if (id == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../faulttype/updateDel/" + id,
                    async: true,
                    /*              params: {"id": id},
                                  type: "POST",*/
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../faulttype/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.faultType = r.faultType;
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#faultTypeGrid").jqGrid('getGridParam', 'page');
            $("#faultTypeGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'id': vm.q.id, 'category': vm.q.category, 'treeCode': vm.q.treeCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');

        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                id: '',
                category: '',
                treeCode: ''
            };
            vm.reload();
            // 取消选中
            faultTree.cancelSelectedNode();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (!res.err) {
                this.$Loading.finish();
                alert('导入成功', function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
    }
});

function getFaultTypeId() {
    var selected = $('#faultTypeGrid').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        iview.Message.error("请选择一条记录");
        return false;
    } else {
        return selected[0].id;
    }
}
